home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Almathera Ten Pack 3: CDPD 3
/
Almathera Ten on Ten - Disc 3: CDPD3.iso
/
scope
/
151-175
/
scopedisk157
/
dsize
/
dsize.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-19
|
7KB
|
231 lines
/*
DSIZE by Gregg A. Reno Downingtown, Pa
Almost all of this code was borrowed from the program TREE by
David A. Dette
This command is designed to get an AmigaDOS directory and
display it and its sub-directories to the screen. It will also display
the disk space used by each directory, including all of the files and
directories below it.
The output may be redirected to the printer by using the
standard cli redirection system but the redirection must follow the
command and preceed any directory or column options.
Syntax: DSIZE <directory> <-max_levels>
where
directory = a AmigaDOS directory or device name
max_leves = the maximum levels of the tree to display.
For example, -1 will just display the directory
specified. -2 will display the directory specified
and any directories below it. No matter what level
number you specify, it will still look at all files
in all of the subdirectories.
*/
#include <exec/types.h>
#include <stdio.h>
#include <ctype.h>
#include <exec/memory.h>
#include <libraries/dos.h>
int max_levels,printdir();
void output();
struct LINK {
struct LINK *next;
char str[120];
};
/* Forground and background colors for each level */
char fg[21] = {31,33,31,33,31,33,31,33,31,33,31,33,31,33,31,33,31,33,31,33,31};
struct LINK *curlink, *lastlink;
char *spaces = " ";
char tstring[128],tstring2[128];
main(argc,argv)
int argc;
char *argv[];
{
char *dirname; /* Pointer to Starting AmigaDOS directory */
int size;
max_levels = 99;
curlink = lastlink = 0; /* Linked list pointers */
switch (argc)
{
case 1 :
dirname = ""; /* No Directory Specified start with current */
break;
case 2 :
if (argv[1][0] == '?')
{
printf("Usage: DSIZE [dirname] {-max levels}\n");
printf("Programmed by: Gregg Reno\n");
Exit();
}
else if (argv[1][0] == '-')
{
max_levels = atoi(argv[1]+1);
dirname = "";
}
else
dirname = argv[1];
break;
case 3 :
if (argv[2][0] == '-')
{
max_levels = atoi(argv[2]+1);
dirname = argv[1];
}
else if (argv[1][0] == '-')
{
max_levels = atoi(argv[1]+1);
dirname = argv[2];
}
else
dirname = argv[1];
break;
default :
printf("Usage: Tree [dirname] {-max levels}\n");
printf("Programmed by: David A. Dette\n");
printf(" Wichita, Kansas\n");
Exit();
}
size = printdir(dirname,0);
while (lastlink != 0)
{
printf("%s",lastlink->str);
curlink = lastlink->next;
FreeMem(lastlink, sizeof(struct LINK));
lastlink = curlink;
}
/* printf("Grand total for %s: %d\n",size);*/
}
int printdir(dirname,level)
char *dirname;
int level; /* Current subdirectory level */
{
char *adddir();
char *subdir[50]; /* Pointer area for Sub-Directories */
char pathname[400]; /* Pointers to AmigaDOS sub-directories */
struct FileLock *dir; /* Locked AmigaDOS directory */
struct FileInfoBlock *fb; /* File Info from locked directory */
UBYTE count; /* Line length count */
UBYTE countdir,indexdir; /* Pending Directories */
long IoErr(); /* Error in file access */
void *AllocMem();
struct FileLock *Lock();
int size,fsize;
countdir = indexdir = 0; /* Zero Directory Entry Counters */
size = fsize = 0; /* Size of directory in blocks */
fb = (struct FileInfoBlock *) AllocMem(sizeof(struct FileInfoBlock),
MEMF_CLEAR | MEMF_PUBLIC);
if ((dir = Lock(dirname,ACCESS_READ)) == NULL)
{
printf("Invalid directory '%s' requested\n",dirname);
Exit();
}
if (!Examine(dir,fb) || (fb->fib_DirEntryType <= 0))
{
printf("Invalid directory '%s' requested\n",dirname);
UnLock(dir);
FreeMem(fb,sizeof(struct FileInfoBlock));
Exit();
}
count = 0;
while (ExNext(dir,fb))
{
if (fb->fib_DirEntryType > 0)
{
subdir[countdir++] = adddir(fb->fib_FileName);
}
else
{
/* printf("%-19s",fb->fib_FileName); */
size += fb->fib_NumBlocks;
fsize += fb->fib_NumBlocks;
}
count++;
}
UnLock(dir);
FreeMem(fb,sizeof(struct FileInfoBlock));
while (indexdir < countdir)
{
strcpy(pathname,dirname);
if ((dirname[strlen(dirname)-1] != ':') &&
(dirname[strlen(dirname)-1] != '/') && (strlen(dirname) > 0))
strcat(pathname,"/");
strcat(pathname,subdir[indexdir]);
size += printdir(pathname,level + 1);
FreeMem(subdir[indexdir],strlen(subdir[indexdir])+1);
indexdir++;
}
if (fsize != 0 && countdir != 0 & (level+1) < max_levels )
output("(FILES)",fsize,level+1);
if (level < max_levels) output(dirname,size,level);
return (size);
}
void output( dir, blocks, levno)
char *dir;
int blocks, levno;
{
int i,len;
double f;
/* Strip off all but last directory name */
len = strlen(dir);
for (i = len; dir[i] != ':' && dir[i] != '/' && i >= 0; --i);
if (i > 0 && i != len-1)
strcpy(tstring, &dir[i+1]);
else
strcpy(tstring,dir);
i = levno * 3; /* Number of spaces to indent */
f = blocks / 2048.0; /* 2048 blocks per meg */
spaces[i] = 0;
sprintf(tstring2,"\033[0;%dm%s%-12s %6d blocks, %3.2lf meg\033[0m\n",
fg[levno],spaces,tstring, blocks, f);
spaces[i] = ' ';
/* Add this string to the linked list */
len = strlen(tstring2);
if (len > 78) tstring[79] = 0;
curlink = AllocMem(sizeof(struct LINK),MEMF_CLEAR | MEMF_PUBLIC);
if (curlink == NULL)
{
printf("Not Enough Memory for output line\n");
return;
}
strcpy(curlink->str, tstring2);
curlink->next = lastlink;
lastlink = curlink;
}
char *adddir(dirname)
char *dirname;
{
char *nameadd; /* Address of the directory name */
nameadd = AllocMem(strlen(dirname)+1,MEMF_CLEAR | MEMF_PUBLIC);
if (nameadd == NULL)
{
printf("Not Enough Memory for Directories!!!\n");
Exit();
}
return((char *)strcpy(nameadd,dirname));
}